from sqlalchemy import ForeignKey, CheckConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from database.models.base import str_256, created_at, updated_at, Base


class Product(Base):
    __tablename__ = "products"
    
    name: Mapped[str_256] = mapped_column(primary_key=True)
    username: Mapped[int] = mapped_column(
        ForeignKey("users.username", ondelete="CASCADE", onupdate="CASCADE"), 
        primary_key=True,
    )
    proteins_100g: Mapped[float]
    fats_100g: Mapped[float]
    carbs_100g: Mapped[float]
    price_100g: Mapped[float]
    ready_to_eat: Mapped[bool]
    max_per_day: Mapped[float | None]
    description: Mapped[str | None]
    
    created_at: Mapped[created_at]
    updated_at: Mapped[updated_at]
    
    user: Mapped["User"] = relationship(  # noqa: F821
        back_populates="products",
    )
    
    __tableargs__ = (
        CheckConstraint("proteins_100g >= 0", name='check_protein_positive'),
        CheckConstraint("fats_100g >= 0", name='check_protein_positive'),
        CheckConstraint("carbs_100g >= 0", name='check_protein_positive'),
        CheckConstraint("price_100g >= 0", name='check_price_positive'),
    )
    